﻿namespace ExamV5.PaperInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmEditUserTemplet : Page
    {
        protected Button butClose;
        protected Button butEnter;
        protected GridView DataGridInfo;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        private PublicFun publicFun_0 = new PublicFun();
        protected TextBox txtJoinUserNum;
        protected TextBox txtTemplateName;

        protected void butEnter_Click(object sender, EventArgs e)
        {
            int num = 0;
            int num2 = 0;
            string str = "";
            string str2 = "";
            string strSql = "";
            try
            {
                num = Convert.ToInt32(this.txtJoinUserNum.Text.Trim());
            }
            catch
            {
                base.Response.Write("<script>alert('抽考人员总数输入不合法！')</script>");
                return;
            }
            try
            {
                for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
                {
                    TextBox box = (TextBox) this.DataGridInfo.Rows[i].Cells[2].FindControl("txtJoinUser");
                    if (!((box.Text.Trim() != "") ? !(box.Text.Trim() != "0") : true))
                    {
                        num2 += Convert.ToInt32(box.Text.Trim());
                        str2 = str2 + box.Text.Trim() + "#";
                        str = str + this.DataGridInfo.DataKeys[i]["ID"].ToString().Trim() + "#";
                    }
                }
            }
            catch
            {
                base.Response.Write("<script>alert('抽考人员人数输入不合法！')</script>");
                return;
            }
            if (num == num2)
            {
                if (this.int_0 != 0)
                {
                    strSql = string.Concat(new object[] { "update ExamUserRule set TemplateName='", this.publicFun_0.CheckString(this.txtTemplateName.Text), "',JoinUserCount=", num, ",WhereName='", str.Trim(), "',WhereValue='", str2.Trim(), "'," });
                    strSql = string.Concat(new object[] { strSql, "InputUser='", this.httpCookie_1.Value, "',InputTime=getdate() where ID=", this.int_0 });
                }
                else
                {
                    strSql = "insert into ExamUserRule(TemplateName,JoinUserCount,WhereName,WhereValue,InputUser,InputTime)";
                    strSql = string.Concat(new object[] { strSql, " values('", this.publicFun_0.CheckString(this.txtTemplateName.Text), "',", num, ",'", str.Trim(), "','", str2.Trim(), "'," }) + "'" + this.httpCookie_1.Value + "',getdate())";
                }
                this.publicFun_0.ExecuteSql(strSql);
                base.Response.Write("<script>alert('编辑参考人员模板操作成功！');window.opener.location.reload();window.close();</script>");
            }
            else
            {
                base.Response.Write("<script>alert('抽考人员总数与人员分布总数不符！')</script>");
            }
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            else
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label label = (Label) e.Row.Cells[1].FindControl("labUserCount");
                string values = this.publicFun_0.GetValues("select Count(*) as UserCount from UserInfo where Flag='A' and UserLevel='A' and DeptID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "UserCount");
                if (!(values.Trim() == ""))
                {
                    label.Text = values + "人";
                }
                else
                {
                    label.Text = "0人";
                }
                if (this.DataGridInfo.DataKeys[e.Row.RowIndex]["UPID"].ToString().Replace("&nbsp;", "") != "0")
                {
                    e.Row.Cells[0].Text = this.publicFun_0.GetValues("select DeptName from DeptInfo where ID=" + this.publicFun_0.CheckString(this.DataGridInfo.DataKeys[e.Row.RowIndex]["UPID"].ToString().Replace("&nbsp;", "")), "DeptName") + "＼" + e.Row.Cells[0].Text;
                }
                TextBox box = (TextBox) e.Row.Cells[2].FindControl("txtJoinUser");
                if (box.Text.Trim() == "")
                {
                    box.Text = "0";
                }
            }
        }

        private void method_0()
        {
            string strSql = "";
            if (!(this.httpCookie_2.Value == "C"))
            {
                strSql = "select b.UPID,b.ID,b.DeptName from DeptInfo b Where " + this.publicFun_0.GetManagDeptID(this.httpCookie_3.Value) + " ";
            }
            else
            {
                strSql = "select b.UPID,b.ID,b.DeptName from DeptInfo b Where b.ID<>0 ";
            }
            strSql = strSql + " order by b.UPID,b.SortInfo,b.ID";
            this.DataGridInfo.DataSource = this.publicFun_0.RetrunDataView(strSql);
            this.DataGridInfo.DataBind();
        }

        private void method_1()
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select * from ExamUserRule where ID=" + Convert.ToInt32(base.Request["ID"].ToString()), connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                this.txtJoinUserNum.Text = reader["JoinUserCount"].ToString();
                this.txtTemplateName.Text = reader["TemplateName"].ToString();
                string[] strArray = reader["WhereName"].ToString().Split(new char[] { '#' });
                string[] strArray2 = reader["WhereValue"].ToString().Split(new char[] { '#' });
                for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
                {
                    TextBox box = (TextBox) this.DataGridInfo.Rows[i].Cells[2].FindControl("txtJoinUser");
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        if (this.DataGridInfo.DataKeys[i]["ID"].ToString().Trim() == strArray[j].Trim())
                        {
                            box.Text = strArray2[j].Trim();
                        }
                    }
                }
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.int_0 = Convert.ToInt32(base.Request["ID"].ToString());
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        this.ViewState["UserPwoer"] = this.publicFun_0.GetValues("select PaperTemple from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "PaperTemple");
                        this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                        if (!(this.ViewState["UserPwoer"].ToString() != "Y"))
                        {
                            this.method_0();
                            this.butEnter.Enabled = true;
                            if (this.int_0 != 0)
                            {
                                this.method_1();
                            }
                        }
                        else
                        {
                            this.butEnter.Enabled = false;
                            base.Response.Write("<script>alert('试卷模板权限未开放！')</script>");
                        }
                    }
                    else
                    {
                        base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

